package com.vic.kjlink.order.service;

import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;

import com.vic.kjlink.order.controller.BaseOrderController;
import com.vic.kjlink.order.lookup.OrdersLookup;
import com.vic.kjlink.order.mapper.OrdersMapper;
import com.vic.kjlink.order.model.Orders;
import com.vic.kjlink.order.model.Start;
import com.vic.kjlink.order.model.User;

import pers.vic.boot.base.model.BaseSelectModel;
import pers.vic.boot.base.model.PageInfo;
import pers.vic.boot.base.service.BaseService;

/**
 * @description:点餐表 Service
 * @author Vic.xu
 * @date: 2021-08-19 13:08
 */
@Service
public class OrdersService extends BaseService<OrdersMapper, Orders> {

    @Resource
    private StartService startService;

    @Resource
    private FoodService foodService;

    @Override
    protected boolean hasAttachment() {
        return false;
    }

    @Override
    public int save(Orders entity) {
        if (entity.getId() == null || entity.getId() <= 0) {
            User user = BaseOrderController.currentUser();
            entity.setDate(new Date());
            entity.setDepartmentName(user.getDepartmentName());
            entity.setUserCode(user.getUserCode());
            entity.setUserName(user.getUserName());
        }
        entity.setDescribe(foodService.findById(entity.getPackageId()).getFullDescribe());
        return super.save(entity);
    }

    /**
     * 今日可点餐 以及已点餐 foodId foodDescribe
     *
     * @return
     */
    public List<BaseSelectModel> todayOrders(Orders orders) {
        Start lastOpen = startService.getLastOpen();
        // 非点餐时间
        if (lastOpen == null || lastOpen.getId() == null) {
            return null;
        }
        List<BaseSelectModel> todayOrders = this.todayOrders(lastOpen, orders);

        return todayOrders;

    }

    /**
     * 今日可点餐 以及已点餐 foodId foodDescribe
     * 
     * @return
     */
    public List<BaseSelectModel> todayOrders(String userCode) {
        Start lastOpen = startService.getLastOpen();
        // 非点餐时间
        if (lastOpen == null || lastOpen.getId() == null) {
            return null;
        }
        Orders orders = getTodayUserOrder(lastOpen.getId(), userCode);
        List<BaseSelectModel> todayOrders = this.todayOrders(lastOpen, orders);

        return todayOrders;

    }

    /**
     * 今日可点餐 以及已点餐
     * 
     * @param lastOpen
     *            开启点餐
     * @param orders
     *            已点的餐
     * @return
     */
    public List<BaseSelectModel> todayOrders(Start lastOpen, Orders orders) {
        List<BaseSelectModel> todayOrders = startService.details(lastOpen.getId()).stream().map(d -> {
            BaseSelectModel model = new BaseSelectModel(d.getFoodId(), d.getFoodDescribe());
            if (orders != null && orders.getPackageId().equals(d.getFoodId())) {
                model.setSelected(true);
            }
            return model;
        }).collect(Collectors.toList());

        return todayOrders;

    }

    /**
     * 今日点的餐
     */
    public Orders getTodayUserOrder(int startId, String userCode) {
        return mapper.getTodayUserOrder(startId, userCode);
    }

    public PageInfo<Orders> page(OrdersLookup lookup) {
        if (lookup.getLatest() != null && lookup.getLatest()) {
            lookup.setStartId(startService.getLast());
        }
        return super.page(lookup);
    }

    public List<Orders> list(OrdersLookup lookup) {
        if (lookup.getLatest() != null && lookup.getLatest()) {
            lookup.setStartId(startService.getLast());
        }
        return super.list(lookup);
    }
}
